{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a1237a5a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "216770e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "#第一问  删除confidence小于0.9的数据  \n",
    "\n",
    "df = pd.read_csv('gaze.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "be89f08d",
   "metadata": {},
   "outputs": [],
   "source": [
    "test1 = df.loc[df['confidence'] >= 0.9]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "417bbfb7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        gaze_timestamp  world_index  confidence  norm_pos_x  norm_pos_y  \\\n",
      "21        88150.825638            2    0.915197    0.493612    0.398950   \n",
      "22        88150.827729            2    0.915197    0.493561    0.399305   \n",
      "23        88150.829663            2    0.915197    0.493570    0.399542   \n",
      "39        88150.866015            3    0.922197    0.492592    0.398705   \n",
      "40        88150.869620            3    0.922197    0.492635    0.398814   \n",
      "...                ...          ...         ...         ...         ...   \n",
      "125259    88443.695600         8788    0.995336    0.407831    0.413470   \n",
      "125260    88443.697570         8788    0.948733    0.407552    0.414731   \n",
      "125261    88443.701580         8788    0.945284    0.407467    0.414705   \n",
      "125262    88443.703608         8788    0.945284    0.407160    0.415340   \n",
      "125263    88443.705574         8788    0.945284    0.407637    0.413674   \n",
      "\n",
      "                                 base_data  gaze_point_3d_x  gaze_point_3d_y  \\\n",
      "21           88150.827729-0 88150.823548-1         2.412810       -21.468145   \n",
      "22           88150.827729-0 88150.827729-1         2.429837       -21.374413   \n",
      "23      88150.827729-0 88150.83159799999-1         2.427118       -21.328193   \n",
      "39      88150.867514-0 88150.86451599999-1         2.827911       -21.750938   \n",
      "40           88150.867514-0 88150.871726-1         2.813774       -21.744854   \n",
      "...                                    ...              ...              ...   \n",
      "125259        88443.69561-0 88443.695591-1       327.648915      -173.027524   \n",
      "125260   88443.69561-0 88443.69953099999-1       311.054477      -161.381388   \n",
      "125261  88443.703629-0 88443.69953099999-1       313.566509      -162.585330   \n",
      "125262       88443.703629-0 88443.703587-1       297.019831      -152.352987   \n",
      "125263  88443.703629-0 88443.70751899999-1       323.629974      -170.142461   \n",
      "\n",
      "        gaze_point_3d_z  eye_center0_3d_x  ...  eye_center0_3d_z  \\\n",
      "21          -295.071057         20.000000  ...        -20.000000   \n",
      "22          -294.819200         20.000000  ...        -20.000000   \n",
      "23          -294.874516         20.000000  ...        -20.000000   \n",
      "39          -298.233245         20.000000  ...        -20.000000   \n",
      "40          -298.471253         20.000000  ...        -20.000000   \n",
      "...                 ...               ...  ...               ...   \n",
      "125259     -2777.243340         19.888755  ...        -20.707271   \n",
      "125260     -2628.637408         19.888755  ...        -20.707271   \n",
      "125261     -2647.420719         19.888755  ...        -20.707271   \n",
      "125262     -2499.428189         19.888755  ...        -20.707271   \n",
      "125263     -2737.404466         19.888755  ...        -20.707271   \n",
      "\n",
      "        gaze_normal0_x  gaze_normal0_y  gaze_normal0_z  eye_center1_3d_x  \\\n",
      "21            0.062883        0.025100        0.997705        -39.934928   \n",
      "22            0.062883        0.025100        0.997705        -39.934928   \n",
      "23            0.062883        0.025100        0.997705        -39.934928   \n",
      "39            0.060717        0.025220        0.997836        -39.934928   \n",
      "40            0.060717        0.025220        0.997836        -39.934928   \n",
      "...                ...             ...             ...               ...   \n",
      "125259       -0.110854        0.041843        0.992955        -37.372594   \n",
      "125260       -0.110854        0.041843        0.992955        -37.372594   \n",
      "125261       -0.111009        0.041794        0.992940        -37.372594   \n",
      "125262       -0.111009        0.041794        0.992940        -37.372594   \n",
      "125263       -0.111009        0.041794        0.992940        -37.372594   \n",
      "\n",
      "        eye_center1_3d_y  eye_center1_3d_z  gaze_normal1_x  gaze_normal1_y  \\\n",
      "21             14.997919        -20.075283       -0.150041        0.234536   \n",
      "22             14.997919        -20.075283       -0.150243        0.234115   \n",
      "23             14.997919        -20.075283       -0.150210        0.233747   \n",
      "39             14.997919        -20.075283       -0.149814        0.233510   \n",
      "40             14.997919        -20.075283       -0.149645        0.233258   \n",
      "...                  ...               ...             ...             ...   \n",
      "125259         14.763454        -23.294339       -0.130868        0.093021   \n",
      "125260         14.763454        -23.294339       -0.132037        0.091860   \n",
      "125261         14.763454        -23.294339       -0.132037        0.091860   \n",
      "125262         14.763454        -23.294339       -0.133311        0.091656   \n",
      "125263         14.763454        -23.294339       -0.131320        0.092941   \n",
      "\n",
      "        gaze_normal1_z  \n",
      "21            0.960458  \n",
      "22            0.960530  \n",
      "23            0.960624  \n",
      "39            0.960744  \n",
      "40            0.960831  \n",
      "...                ...  \n",
      "125259        0.987026  \n",
      "125260        0.986979  \n",
      "125261        0.986979  \n",
      "125262        0.986827  \n",
      "125263        0.986974  \n",
      "\n",
      "[87442 rows x 21 columns]\n"
     ]
    }
   ],
   "source": [
    "#test1.to_csv('test.csv') 单独创建新的csv文件\n",
    "print(test1) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "35be39da",
   "metadata": {},
   "outputs": [],
   "source": [
    "#t = pd.read_csv('test.csv')  生成新的csv文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "bb879632",
   "metadata": {},
   "outputs": [],
   "source": [
    "#第二问  删除无效数据\n",
    "\n",
    "t = test1\n",
    "norm_x = t['norm_pos_x']\n",
    "norm_y = t['norm_pos_y']\n",
    "mean_x = np.mean(norm_x)\n",
    "mean_y = np.mean(norm_y)\n",
    "std_x = np.std(norm_x)\n",
    "std_y = np.std(norm_y)\n",
    "x1 = mean_x - 3 * std_x\n",
    "x2 = mean_x + 3 * std_x\n",
    "y1 = mean_y - 3 * std_y\n",
    "y2 = mean_y + 3 * std_y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e0baaecf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        gaze_timestamp  world_index  confidence  norm_pos_x  norm_pos_y  \\\n",
      "21        88150.825638            2    0.915197    0.493612    0.398950   \n",
      "22        88150.827729            2    0.915197    0.493561    0.399305   \n",
      "23        88150.829663            2    0.915197    0.493570    0.399542   \n",
      "39        88150.866015            3    0.922197    0.492592    0.398705   \n",
      "40        88150.869620            3    0.922197    0.492635    0.398814   \n",
      "...                ...          ...         ...         ...         ...   \n",
      "125259    88443.695600         8788    0.995336    0.407831    0.413470   \n",
      "125260    88443.697570         8788    0.948733    0.407552    0.414731   \n",
      "125261    88443.701580         8788    0.945284    0.407467    0.414705   \n",
      "125262    88443.703608         8788    0.945284    0.407160    0.415340   \n",
      "125263    88443.705574         8788    0.945284    0.407637    0.413674   \n",
      "\n",
      "                                 base_data  gaze_point_3d_x  gaze_point_3d_y  \\\n",
      "21           88150.827729-0 88150.823548-1         2.412810       -21.468145   \n",
      "22           88150.827729-0 88150.827729-1         2.429837       -21.374413   \n",
      "23      88150.827729-0 88150.83159799999-1         2.427118       -21.328193   \n",
      "39      88150.867514-0 88150.86451599999-1         2.827911       -21.750938   \n",
      "40           88150.867514-0 88150.871726-1         2.813774       -21.744854   \n",
      "...                                    ...              ...              ...   \n",
      "125259        88443.69561-0 88443.695591-1       327.648915      -173.027524   \n",
      "125260   88443.69561-0 88443.69953099999-1       311.054477      -161.381388   \n",
      "125261  88443.703629-0 88443.69953099999-1       313.566509      -162.585330   \n",
      "125262       88443.703629-0 88443.703587-1       297.019831      -152.352987   \n",
      "125263  88443.703629-0 88443.70751899999-1       323.629974      -170.142461   \n",
      "\n",
      "        gaze_point_3d_z  eye_center0_3d_x  ...  eye_center0_3d_z  \\\n",
      "21          -295.071057         20.000000  ...        -20.000000   \n",
      "22          -294.819200         20.000000  ...        -20.000000   \n",
      "23          -294.874516         20.000000  ...        -20.000000   \n",
      "39          -298.233245         20.000000  ...        -20.000000   \n",
      "40          -298.471253         20.000000  ...        -20.000000   \n",
      "...                 ...               ...  ...               ...   \n",
      "125259     -2777.243340         19.888755  ...        -20.707271   \n",
      "125260     -2628.637408         19.888755  ...        -20.707271   \n",
      "125261     -2647.420719         19.888755  ...        -20.707271   \n",
      "125262     -2499.428189         19.888755  ...        -20.707271   \n",
      "125263     -2737.404466         19.888755  ...        -20.707271   \n",
      "\n",
      "        gaze_normal0_x  gaze_normal0_y  gaze_normal0_z  eye_center1_3d_x  \\\n",
      "21            0.062883        0.025100        0.997705        -39.934928   \n",
      "22            0.062883        0.025100        0.997705        -39.934928   \n",
      "23            0.062883        0.025100        0.997705        -39.934928   \n",
      "39            0.060717        0.025220        0.997836        -39.934928   \n",
      "40            0.060717        0.025220        0.997836        -39.934928   \n",
      "...                ...             ...             ...               ...   \n",
      "125259       -0.110854        0.041843        0.992955        -37.372594   \n",
      "125260       -0.110854        0.041843        0.992955        -37.372594   \n",
      "125261       -0.111009        0.041794        0.992940        -37.372594   \n",
      "125262       -0.111009        0.041794        0.992940        -37.372594   \n",
      "125263       -0.111009        0.041794        0.992940        -37.372594   \n",
      "\n",
      "        eye_center1_3d_y  eye_center1_3d_z  gaze_normal1_x  gaze_normal1_y  \\\n",
      "21             14.997919        -20.075283       -0.150041        0.234536   \n",
      "22             14.997919        -20.075283       -0.150243        0.234115   \n",
      "23             14.997919        -20.075283       -0.150210        0.233747   \n",
      "39             14.997919        -20.075283       -0.149814        0.233510   \n",
      "40             14.997919        -20.075283       -0.149645        0.233258   \n",
      "...                  ...               ...             ...             ...   \n",
      "125259         14.763454        -23.294339       -0.130868        0.093021   \n",
      "125260         14.763454        -23.294339       -0.132037        0.091860   \n",
      "125261         14.763454        -23.294339       -0.132037        0.091860   \n",
      "125262         14.763454        -23.294339       -0.133311        0.091656   \n",
      "125263         14.763454        -23.294339       -0.131320        0.092941   \n",
      "\n",
      "        gaze_normal1_z  \n",
      "21            0.960458  \n",
      "22            0.960530  \n",
      "23            0.960624  \n",
      "39            0.960744  \n",
      "40            0.960831  \n",
      "...                ...  \n",
      "125259        0.987026  \n",
      "125260        0.986979  \n",
      "125261        0.986979  \n",
      "125262        0.986827  \n",
      "125263        0.986974  \n",
      "\n",
      "[86185 rows x 21 columns]\n"
     ]
    }
   ],
   "source": [
    "test2 = t.loc[(t['norm_pos_x'] > x1) & (t['norm_pos_x'] < x2) & (t['norm_pos_y'] > y1) & (t['norm_pos_y'] < y2)]\n",
    "#test2.to_csv('test1.csv')   建立第二问的新文件\n",
    "print(test2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "935ce54e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                      gaze_timestamp  world_index  confidence  norm_pos_x  \\\n",
      "21     1970-01-01 00:00:00.000088150            2    0.915197    0.493612   \n",
      "22     1970-01-01 00:00:00.000088150            2    0.915197    0.493561   \n",
      "23     1970-01-01 00:00:00.000088150            2    0.915197    0.493570   \n",
      "39     1970-01-01 00:00:00.000088150            3    0.922197    0.492592   \n",
      "40     1970-01-01 00:00:00.000088150            3    0.922197    0.492635   \n",
      "...                              ...          ...         ...         ...   \n",
      "125259 1970-01-01 00:00:00.000088443         8788    0.995336    0.407831   \n",
      "125260 1970-01-01 00:00:00.000088443         8788    0.948733    0.407552   \n",
      "125261 1970-01-01 00:00:00.000088443         8788    0.945284    0.407467   \n",
      "125262 1970-01-01 00:00:00.000088443         8788    0.945284    0.407160   \n",
      "125263 1970-01-01 00:00:00.000088443         8788    0.945284    0.407637   \n",
      "\n",
      "        norm_pos_y                           base_data  gaze_point_3d_x  \\\n",
      "21        0.398950       88150.827729-0 88150.823548-1         2.412810   \n",
      "22        0.399305       88150.827729-0 88150.827729-1         2.429837   \n",
      "23        0.399542  88150.827729-0 88150.83159799999-1         2.427118   \n",
      "39        0.398705  88150.867514-0 88150.86451599999-1         2.827911   \n",
      "40        0.398814       88150.867514-0 88150.871726-1         2.813774   \n",
      "...            ...                                 ...              ...   \n",
      "125259    0.413470        88443.69561-0 88443.695591-1       327.648915   \n",
      "125260    0.414731   88443.69561-0 88443.69953099999-1       311.054477   \n",
      "125261    0.414705  88443.703629-0 88443.69953099999-1       313.566509   \n",
      "125262    0.415340       88443.703629-0 88443.703587-1       297.019831   \n",
      "125263    0.413674  88443.703629-0 88443.70751899999-1       323.629974   \n",
      "\n",
      "        gaze_point_3d_y  gaze_point_3d_z  eye_center0_3d_x  ...  \\\n",
      "21           -21.468145      -295.071057         20.000000  ...   \n",
      "22           -21.374413      -294.819200         20.000000  ...   \n",
      "23           -21.328193      -294.874516         20.000000  ...   \n",
      "39           -21.750938      -298.233245         20.000000  ...   \n",
      "40           -21.744854      -298.471253         20.000000  ...   \n",
      "...                 ...              ...               ...  ...   \n",
      "125259      -173.027524     -2777.243340         19.888755  ...   \n",
      "125260      -161.381388     -2628.637408         19.888755  ...   \n",
      "125261      -162.585330     -2647.420719         19.888755  ...   \n",
      "125262      -152.352987     -2499.428189         19.888755  ...   \n",
      "125263      -170.142461     -2737.404466         19.888755  ...   \n",
      "\n",
      "        eye_center0_3d_z  gaze_normal0_x  gaze_normal0_y  gaze_normal0_z  \\\n",
      "21            -20.000000        0.062883        0.025100        0.997705   \n",
      "22            -20.000000        0.062883        0.025100        0.997705   \n",
      "23            -20.000000        0.062883        0.025100        0.997705   \n",
      "39            -20.000000        0.060717        0.025220        0.997836   \n",
      "40            -20.000000        0.060717        0.025220        0.997836   \n",
      "...                  ...             ...             ...             ...   \n",
      "125259        -20.707271       -0.110854        0.041843        0.992955   \n",
      "125260        -20.707271       -0.110854        0.041843        0.992955   \n",
      "125261        -20.707271       -0.111009        0.041794        0.992940   \n",
      "125262        -20.707271       -0.111009        0.041794        0.992940   \n",
      "125263        -20.707271       -0.111009        0.041794        0.992940   \n",
      "\n",
      "        eye_center1_3d_x  eye_center1_3d_y  eye_center1_3d_z  gaze_normal1_x  \\\n",
      "21            -39.934928         14.997919        -20.075283       -0.150041   \n",
      "22            -39.934928         14.997919        -20.075283       -0.150243   \n",
      "23            -39.934928         14.997919        -20.075283       -0.150210   \n",
      "39            -39.934928         14.997919        -20.075283       -0.149814   \n",
      "40            -39.934928         14.997919        -20.075283       -0.149645   \n",
      "...                  ...               ...               ...             ...   \n",
      "125259        -37.372594         14.763454        -23.294339       -0.130868   \n",
      "125260        -37.372594         14.763454        -23.294339       -0.132037   \n",
      "125261        -37.372594         14.763454        -23.294339       -0.132037   \n",
      "125262        -37.372594         14.763454        -23.294339       -0.133311   \n",
      "125263        -37.372594         14.763454        -23.294339       -0.131320   \n",
      "\n",
      "        gaze_normal1_y  gaze_normal1_z  \n",
      "21            0.234536        0.960458  \n",
      "22            0.234115        0.960530  \n",
      "23            0.233747        0.960624  \n",
      "39            0.233510        0.960744  \n",
      "40            0.233258        0.960831  \n",
      "...                ...             ...  \n",
      "125259        0.093021        0.987026  \n",
      "125260        0.091860        0.986979  \n",
      "125261        0.091860        0.986979  \n",
      "125262        0.091656        0.986827  \n",
      "125263        0.092941        0.986974  \n",
      "\n",
      "[87442 rows x 21 columns]\n"
     ]
    }
   ],
   "source": [
    "#第三问  修改时间戳为日期格式\n",
    "\n",
    "test2 = test1.copy()\n",
    "test2.loc[:,'gaze_timestamp'] = pd.to_datetime(test2.loc[:,'gaze_timestamp']) #修改时间\n",
    "print(test2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "fe56469a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "427.62610766083765\n"
     ]
    }
   ],
   "source": [
    "#第四问  计算采样率\n",
    "\n",
    "num = len(df)\n",
    "time1 = df['gaze_timestamp'][0]\n",
    "time2 = df['gaze_timestamp'][125263]\n",
    "rate = num / (time2 - time1)\n",
    "print(rate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "78afa53e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[88150.77678, 88150.787485, 88150.79840649999, 88150.80726949999, 88150.818051, 88150.827729, 88150.83957499999, 88150.84950499999, 88150.85956099999, 88150.86962, 88150.87958449998, 88150.88999649999, 88150.8995115, 88150.90939799999, 88150.91942349999, 88150.9275245, 88150.93855949999, 88150.947523, 88150.95950249999, 88150.96950749999, 88150.97952299999]\n"
     ]
    }
   ],
   "source": [
    "#第五问  重新采样\n",
    "\n",
    "temp = df['gaze_timestamp'][0]\n",
    "res = []\n",
    "res.append(df['gaze_timestamp'][0])\n",
    "i = 1\n",
    "fre = 0.01\n",
    "diff = flag = 1\n",
    "while len(res) < 21 :\n",
    "    diff = abs(float(df['gaze_timestamp'][i] - temp - fre))\n",
    "    if diff >= flag:\n",
    "        diff = 1\n",
    "        res.append(df['gaze_timestamp'][i-1])\n",
    "        temp = df['gaze_timestamp'][i-1]\n",
    "    i += 1\n",
    "    flag = diff\n",
    "\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed04d327",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
